{% include 'header.html' %}

  <title>Wox Execution</title>
  <link href="{{ STATIC_URL }}css/validationEngine.jquery.css" rel="stylesheet" type="text/css">
  <link href="{{ STATIC_URL }}css/jquery-ui-1.9.2.custom.min.css" rel="stylesheet" type="text/css">
  <link href="{{ STATIC_URL }}css/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery-ui-1.9.2.custom.min.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery-ui-timepicker-addon.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.validationEngine.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.validationEngine-en.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.blockUI.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/mustache.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery-Mustache.js"></script>

  <script type="text/javascript"> 

    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    function showLoading(obj) {
        if (obj) {
            var nextobj = obj.next('.field-indic');
            if (nextobj)
                nextobj.attr('class', 'field-indic loading-indicator');
        }
    }

    function endLoading(obj) {
        if (obj) {
            var nextobj = obj.next('.field-indic');
            if (nextobj)
                nextobj.attr('class', 'field-indic ');
        }
    }

    Mustache.tags = ['[[', ']]'];      
    var csrftoken = getCookie('csrftoken');

    $(document).ready(function() {

{% include 'topnav-imd.js' %}

        $('#form_{{dj_exec}}').validationEngine('attach', {scroll: false});
        $('body').mousemove(function() {
            $('#form_{{dj_exec}}').validationEngine('hideAll');
        });

        var d = new Date();
        // the job should be scheduled at least in 1 hour
        d.setHours(d.getHours() + 1);
        $( "#inp_start_date" ).datetimepicker({
            minDate: d,
            dateFormat: 'yy-mm-dd',
            timeFormat: 'HH:mm z',
            showTimezone: true,
            timezone: 'CST',
            timezoneList: [ 
                { value: 'CST', label: 'PRC'}, 
                { value: 'GMT', label: 'Ireland' },
                { value: 'PST', label: 'San Diego/US' }
            ],
            onSelect: function() { 
                $('#hid_start_date_utc').attr('value', $(this).datepicker( 'getDate' ).toUTCString());
            }
        }); //end of datetimepicker

        $("input[name='rdo_start_type']").click(function() {
            var inp_nbafter = $('#inp_nbafter');
            var inp_start_date = $('#inp_start_date');
            var val = $(this).val();
            switch (parseInt(val)) {
                case 1:
                case 2:
                    inp_nbafter.attr('value', '');
                    inp_nbafter.attr('class', '');
                    inp_start_date.attr('value', '');
                    inp_start_date.attr('class', '');
                    break;
                case 3:
                    inp_nbafter.focus();
                    inp_nbafter.attr('class', 'validate[required,custom[onlyNumberSp]]');
                    inp_start_date.attr('value', '');
                    inp_start_date.attr('class', '');
                    break;
                case 4:
                    inp_start_date.focus();
                    inp_start_date.attr('class', 'validate[required]');
                    inp_nbafter.attr('value', '');
                    inp_nbafter.attr('class', '');
                    break;
            }

            // update class start_type_N
            var newclass = '';
            $('.bz_bugitem').each(function() {
                newclass = $(this).attr('class').replace(/start_type_\d+/, 'start_type_' + val);
                $(this).attr('class', newclass);
            });

        }); // end of click


        $('#sel_mgroup').change(function() {
            if($(this).val() == '') {
                return;
            }

            var container = $('#div_machine_list');
            $.ajax({
                type: 'post',
                url: '{%url ajax_getmlst dj_exec%}',
                dataType: 'json',
                data: $('#form_{{dj_exec}}').serializeArray(),
                async: false,
                crossDomain: false,
                beforeSend: function(xhr, settings) {
                    if (!csrfSafeMethod(settings.type)) {
                        xhr.setRequestHeader("X-CSRFToken", csrftoken);
                    }
                },
                success: function(r) {
                    container.html('<br>');
                    container.attr('class', 'loading-indicator');
                    setTimeout(function() {
                        container.attr('class', '');
                        $.Mustache.addFromDom('tr_testrun_tmpl');
                        $('#tr_testrun').mustache(
                            'tr_testrun_tmpl', 
                            {m_testruns: r.testruns, 
                             'm_testruns_len?': r.testruns ? r.testruns.length : null}, 
                            {method: 'html'});
                        container.html(r.ml);
                    }, 500);
                },
                error: function() {
                    alert("get machine list failed!");
                }
            }); // end of ajax
        }); // end of change

       
        $('#div_machine_list').on('change', "input[name='ckb_machine']", function(e) { 
            if ($("input[name='rdo_start_type']:checked").val() != '1' && 
                $(this).is(':checked') && 
                $(this).next("[name='hid_isai']").val() == '0') {
                alert("Please enable autoinstall on the machine to schedule the test!");
                $(this).attr('checked', false);
            }
        });

        $('#div_machine_list').on('click', '#btn_submitjob', function(e) { 
            e.preventDefault(); 

            $('#form_{{dj_exec}}').ajaxSubmit({
                dataType: 'json',
                beforeSerialize:function() {
                    if (! $('#form_{{dj_exec}}').validationEngine('validate')) {
                        return false;
                    }

                    $.blockUI({ message: '<div id="div_blockbox" class="blockbox_wait"><h3>Just a moment...</h3></div>'});
                },
                success:function(r) {
                    switch (r.type) {
                      case 'alert': 
                      case 'info': 
                        $('#div_blockbox').attr('class', 'blockbox_' + r.type);
                        $('#div_blockbox').html(r.content);
                        $('.blockOverlay').attr('title','click to go back').click($.unblockUI);
                        break;
                      case 'redirect': 
                        // redirect to status tracking page
                        location = r.content;
                        break;
                    } // end of switch
                },
                error:function() {
                    $.unblockUI();
                    alert("submit form_{{dj_exec}} failed!");
                }
            });  // end of ajaxSubmit
        }); // end of on

    }); // end of ready
  </script>

  <script id="tr_testrun_tmpl" type="text/html">
    [[#m_testruns_len?]]
      <td class="th-label">Test Run:</td>
      <td><select multiple id="sel_testrun" name="sel_testrun" style="margin-left:10px;width:100%;" class="validate[required]">
      [[#m_testruns]]
        <option value="[[id]]" selected>[[name]]</option>
      [[/m_testruns]]
      </select><span class="field-indic"></span></td>
    [[/m_testruns_len?]]
  </script>

</head>

<body class="defect-opensolaris-org-bz">
{% include 'topnav.html' %}
<div>
  <table id="tabtitles" border="0" cellpadding="0" cellspacing="0">
  <tbody><tr>
    <td id="title">
    <p><font color="#FFFFFF">&nbsp;Execution </font></p>
    </td></tr>
  </tbody></table>
</div>

<div id="bugzilla-body" style="min-height: 450px;">
 
<form id="form_{{dj_exec}}" method="POST" action="{% url do_exec %}">
<div id="div_search">
{% csrf_token %}
  <table border="0" align="left" class="bz_buglist">
    <tr>
    <td class="th-label">When to start:</td>
    <td><fieldset style="margin-left:10px;">
      <table>
      <tr>
      <td><input type="radio" name="rdo_start_type" value="1" class="validate[required]" checked/></td>
      <td>right now</td>
      <td></td>
      </tr>

      <tr>
      <td><input type="radio" name="rdo_start_type" value="2" class="validate[required]"/></td>
      <td>next build</td>
      <td></td>
      </tr>

      <tr>
      <td><input type="radio" name="rdo_start_type" id="rdo_start_type_3" value="3" class="validate[required]"/></td>
      <td>next build after</td>
      <td><input type="text" name="inp_nbafter" id="inp_nbafter" value="" class=""
           onclick="document.getElementById('rdo_start_type_3').click();"/> hour(s)</td>
      </tr>

      <tr>
      <td><input type="radio" name="rdo_start_type" id="rdo_start_type_4" value="4" class="validate[required]"/></td>
      <td>specify a date</td>
      <td><input type="text" name="inp_start_date" id="inp_start_date" value="" class=""
           onclick="document.getElementById('rdo_start_type_4').click();"/>
          <input type="hidden" name="hid_start_date_utc" id="hid_start_date_utc" value=""/>
      </td>
      </tr> 
      </table>
    </fieldset></td></tr>

    <tr>
      <td class="th-label">Machine Group:</td>
      <td><select id="sel_mgroup" name="sel_mgroup" style="margin-left:10px;width:100%;">
        <option value=""></option>
      {% for g in dj_mgroup_qs %}
        <option value="{{g.id}}">{{g.name}}</option>
      {% endfor %}
      </select><span class="field-indic"></span></td>
    </tr>
    <tr id="tr_testrun">
    </tr>

  </table>
</div>
<!-- end of div_search -->

<br>
<table class="bz_buglist" cellpadding="4" cellspacing="0" width="100%">
<tbody>
</tbody></table>
<div><br></div>

<div id="div_machine_list">
<!-- ajax -->
</div>
<!-- end of div_machine_list -->

</form>

</div>
<!-- end of div bugzilla-body -->


{% include 'footer.html' %}
